<template>
  <el-dialog
    title="新增员工"
    :visible="showDialog"
    top="5vh"
    @close="handleClose"
    @open="handleOpen"
  >
    <!-- 表单 -->
    <el-form label-width="120px">
      <el-form-item label="姓名">
        <el-input
          v-model="employeeForm.username"
          style="width:80%"
          placeholder="请输入姓名"
        />
      </el-form-item>
      <el-form-item label="手机">
        <el-input
          v-model="employeeForm.mobile"
          style="width:80%"
          placeholder="请输入手机号"
        />
      </el-form-item>
      <el-form-item label="入职时间">
        <el-date-picker
          v-model="employeeForm.timeOfEntry"
          style="width:80%"
          placeholder="请选择入职时间"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="聘用形式">
        <el-select
          v-model="employeeForm.formOfEmployment"
          style="width:80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in hireTypes"
            :key="item.id"
            :value="item.id"
            :label="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号">
        <el-input
          v-model="employeeForm.workNumber"
          style="width:80%"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="部门">
        <!--
            readonly: 只读
            disabled: 禁用
         -->
        <el-input
          v-model="employeeForm.departmentName"
          readonly
          style="width:80%"
          placeholder="请选择部门"
          @focus="isShow = true"
        />
        <!-- 部门树形组件 -->
        <el-tree
          v-if="isShow"
          :data="deptTreeData"
          :props="{ label: 'name' }"
          @node-click="handleNodeClick"
        />
      </el-form-item>
      <el-form-item label="转正时间">
        <el-date-picker
          v-model="employeeForm.correctionTime"
          style="width:80%"
          placeholder="请选择转正时间"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleAdd">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>

// 导入常量模块
import obj from '@/constant/employees'
import { getDeptListAPI } from '@/api/departments'
import { addEmployeeAPI } from '@/api/employees'
import { arr2Tree } from '@/utils/shift'
export default {
  name: 'AddEmployee',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 聘用形式列表
      hireTypes: obj.hireType,
      // 部门树形数据
      deptTreeData: [],
      // 控制树形组件的显示
      isShow: false,
      // 员工表单
      employeeForm: {
        username: '',
        mobile: '',
        formOfEmployment: 1,
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
    }
  },

  methods: {
    // 弹框关闭时
    handleClose() {
      this.$emit('update:showDialog', false)
    },
    // 弹框打开时
    async handleOpen() {
      // 发起获取部门列表请求
      const resp = await getDeptListAPI()
      // 转为树形结构再赋值
      this.deptTreeData = arr2Tree(resp.data.depts, '')
    },
    // 树形节点点击时
    handleNodeClick(node) {
      // node: 被点击的部门(节点)对象
      // 如果有子部门，直接结束
      if (node.children) return
      // 否则赋值当前选择的部门
      this.employeeForm.departmentName = node.name
      // 隐藏树形组件
      this.isShow = false
    },
    // 新增员工
    async handleAdd() {
      // 整体校验
      // 发起新增请求
      await addEmployeeAPI(this.employeeForm)
      // 关闭弹框
      this.handleClose()
      // 给个成功的提示
      this.$message.success('新增成功')
      // 获取员工列表
      // 方式1：$parent(不推荐，不安全)
      // this.$parent.getEmployeeList()
      // 方式2：推荐子传父($emit)
      this.$emit('add-success')
    }
  }
}
</script>

<style lang="scss" scoped>
.el-tree {
  position: absolute;
  width: 80%;
  min-height: 50px;
  left: 0;
  top: 45px;
  z-index: 100;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding-right: 5px;
  overflow: hidden;
  background-color: #fff;
  max-height: 200px;
  overflow: auto;
  ::v-deep {
    .el-tree-node__content {
      height: auto;
    }
  }
}
</style>
